package com.echatim.sdk;

public class Main {
    private static boolean sdkInitFinished = false;
    private static int apiCallId = 1; // api 调用id, 实现方式可用时间戳, 或一个自增变量


    static void sdkTestApis() throws InterruptedException {
        // 测试 'userGet'
        while (true){
//            EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "userGet", "{\"auid\":\"admin\"}");

//        //  测试API
        EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "userUpdate", "{}");
        EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "userList", "{}");
        EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "userGet", "{}");
        EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "userUpdateToken", "{}");
        EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "userRefreshToken", "{}");
        EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "historyListMessage", "{}");
        EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "historyListSession", "{}");
        EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "addFriend", "{}");
        EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "delFriend", "{}");
        EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "listFriends", "{}");
        EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "getFriend", "{}");
        EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "modifyBlacklistForbid", "{}");
        EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "listBlackListForbid", "{}");
        EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "modifyAlias", "{}");
        EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "addRoom", "{}");
        EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "updateRoom", "{}");
        EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "listRoom", "{}");
        EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "listRoomJoin", "{}");
        EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "delRoom", "{}");
        EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "getRoom", "{}");
        EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "listRoomMember", "{}");
        EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "addRoomMember", "{}");
        EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "delRoomMember", "{}");
        EChatIMSDKBridge.sdkInvokeApi(apiCallId++, "userOnline", "{}");

            Thread.sleep(10*1000);
        }

    }

    static void sdkFileServerTestApis() {
       long handleId =  EChatIMSDKBridge.sdkFileServerInstance();
       if(handleId == 0){
           System.err.println("couldn't instance fileserver");
           return;
       }
        String UPLOAD_CONFIG = "{\"maxFileSize\":100,\"allowSuffix\":[\"jpeg\",\"zip\",\"mp3\"],\"type\":\"FILE\"}";
        String FILE_PATH = "C:\\Users\\Administrator\\Desktop\\upload\\b.zip";
        EChatIMSDKBridge.sdkFileServerInit(handleId, UPLOAD_CONFIG);
        EChatIMSDKBridge.sdkFileServerSetUploadListener(handleId, (handleId1, method, argv) -> {
            System.out.println(String.format("fileserver upload listener: 0x%x, method: %s, argv[0]:%s, argv[1]:%s", handleId1, method, argv[0], argv[1]));
       });

        int subThead = 0;
        EChatIMSDKBridge.sdkFileServerInvokeUpload(handleId, FILE_PATH, subThead);
        if(subThead == 0){
            EChatIMSDKBridge.sdkFileServerRelease(handleId);
        }
    }


    public static void main(String[] args) throws InterruptedException {
//        String configJson = "{\"host\":\"api.echatim.cn\",\"httpPort\":58082,\"socketPort\":59092,\"key\":\"TSDKTEST00001\",\"secret\":\"\",\"apiTransport\":\"HTTP\",\"loginAuid\":\"admin\",\"loginToken\":\"admin\"}";
        String configJson = "{\"host\":\"api.echatim.cn\",\"httpPort\":58082,\"socketPort\":59092,\"key\":\"TSDKTEST00001\",\"secret\":\"\",\"apiTransport\":\"HTTP\",\"loginAuid\":\"admin\",\"loginToken\":\"admin\",\"fileServerConfig\":{\"use\":\"local\",\"client\":\"curl\",\"baseUrl\":\"http://api.echatim.cn:58082\",\"version\":\"v1\"}}";
        EChatIMSDKBridge.sdkInit(configJson, (ret, errMsg)->{
            if(ret == 0){
                sdkInitFinished = true;
            }
        }, (message, argv) -> {
            if(!"".equals(message) && argv.length > 0){
                System.out.println(String.format("sdk_listener_callback_handler: message = %s, argv[0]=%s", message, argv[0]));
            }
        });

        // 等待sdk初始完成.
        while (!sdkInitFinished){
            Thread.sleep(100);
            System.out.print(".");
        }
        // api 回调处理
        EChatIMSDKBridge.sdkSetApiListener((callId, method, jsonParams) -> {
            if(!"".equals(method) && !"".equals(jsonParams)){
                System.out.println(String.format("api_listener_callback_handler: callId=%d, method = %s, jsonParams=%s", callId, method, jsonParams));
            }
        });

//        sdkTestApis();
        sdkFileServerTestApis();
    }

}
